﻿using IDLETD.Agent;
using IDLETD.FSM;
using IDLETD.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IDLETD.UserControls
{
    /// <summary>
    /// TowerControl.xaml 的交互逻辑
    /// </summary>
    public partial class TowerControl : BaseControl
    {
        #region 依赖属性
        public override BaseGameObject DataInfo
        {
            get { return (BaseGameObject)GetValue(TowerProperty); }
            set { SetValue(TowerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Monster.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TowerProperty =
            DependencyProperty.Register("Tower", typeof(BaseGameObject), typeof(TowerControl), new PropertyMetadata(null, (sender, e) =>
            {
                var dp = sender as TowerControl;
                if (dp == null) return;
                dp.GetDataContent();
            }));

        #endregion

        public event Action<BulletModel, MonsterModel> TowerShotBullet;

        SportAgent sportAgent = new SportAgent();
        public CrashAgent crashAgnet = new CrashAgent();

        private void GetDataContent()
        {
            DataContext = DataInfo;
            sportAgent.Onwer = DataInfo;
            crashAgnet.Onwer = DataInfo;
            DataInfo.PropertyChanged += Monster_PropertyChanged;
            DataInfo.ShotBullet += Tower_ShotBullet;
            DataInfo.RegisterState(StateEnum.空闲, new StateIdle() { Onwer = DataInfo, SportAgent = sportAgent,  });
            DataInfo.RegisterState(StateEnum.扫描, new StateScan() { Onwer = DataInfo, SportAgent = sportAgent, CrashAgent = crashAgnet });
            DataInfo.RegisterState(StateEnum.攻击, new StateAttack() { Onwer = DataInfo, SportAgent = sportAgent });
            DataInfo.RegisterState(StateEnum.冷却, new StateCool() { Onwer = DataInfo, SportAgent = sportAgent });
            DataInfo.GoIdle();
        }

        

        public TowerControl()
        {
            InitializeComponent();
        }

        private void Monster_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "X":
                    Canvas.SetLeft(this, DataInfo.X);
                    break;
                case "Y":
                    Canvas.SetTop(this, DataInfo.Y);
                    break;
            }
        }

        private void Tower_ShotBullet(BulletModel bullet, MonsterModel monster)
        {
            if (TowerShotBullet != null) TowerShotBullet(bullet, monster);
        }

        #region 状态相关
        public void Excute()
        {
            DataInfo.Excute();
        }

        public void GoScan()
        {
            DataInfo.GoScan();
        }

        #endregion
    }
}
